package com.xique.park.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.constant.CacheConstants;
import com.xique.common.core.utils.StringUtils;
import com.xique.park.bean.domain.Area;
import com.xique.park.mapper.AreaMapper;
import com.xique.park.service.service.IAreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地区码服务
 *
 * @author wld
 * @date 2022/3/11 9:18
 */
@Service
@RequiredArgsConstructor
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements IAreaService {

    private final RedisTemplate<Object, Object> redisTemplate;

    @Override
    public List<Area> selectAllProvinceCode() {
        LambdaQueryWrapper<Area> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Area::getLevel, 1);
        return list(queryWrapper);
    }

    @Override
    public List<Area> selectCityByProvince(Integer province) {
        // 获取对应省份的ID
        Area area = getOneByAreaCode(String.valueOf(province));
        long parentId = area.getAreaId();
        // 根据省份ID去查询市列表
        LambdaQueryWrapper<Area> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Area::getParentId, parentId);
        return list(queryWrapper);
    }

    @Override
    public List<Area> selectDistrictByCity(Integer city) {
        // 获取对应市的ID
        Area area = getOneByAreaCode(String.valueOf(city));
        long parentId = area.getAreaId();
        // 根据市ID去查询区列表
        LambdaQueryWrapper<Area> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Area::getParentId, parentId);
        return list(queryWrapper);
    }

    @Override
    public List<Area> all() {
        // 先从缓存中获取
        String result = (String) redisTemplate.opsForValue().get(CacheConstants.AREA_KEY);
        if (StringUtils.isNotEmpty(result)) {
            return JSON.parseArray(result, Area.class);
        }
        // 重建缓存
        // 获取所有的省
        List<Area> provinceList = selectAllProvinceCode();
        provinceList.forEach(province -> {
            // 获取省下所有的市
            List<Area> cityList = selectCityByProvince(Integer.parseInt(province.getAreaCode()));
            province.setChildArea(cityList);
            cityList.forEach(city -> {
                // 获取市下所有的区县
                List<Area> districtList = selectDistrictByCity(Integer.parseInt(city.getAreaCode()));
                city.setChildArea(districtList);
            });
        });
        redisTemplate.opsForValue().set(CacheConstants.AREA_KEY, JSON.toJSON(provinceList).toString());
        return provinceList;
    }

    @Override
    public Area getOneByAreaCode(String areaCode) {
        LambdaQueryWrapper<Area> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Area::getAreaCode, areaCode);
        return getOne(queryWrapper);
    }

}
